<?php

class Admin_NhomVanBanController extends Zend_Controller_Action
{
	public function init()
	{
		$this->_helper->layout->setLayout('admin');
	}
	
	public function indexAction()
	{
		$nhomvanbanMapper = Library_Model_NhomVanBanMapper::getInstance();
		$nhomvanban = $nhomvanbanMapper->nhomvanbanList();
		$this->view->nhomvanban = $nhomvanban;
		
		
	}
	
	public function themAction()
	{
		$this->view->title = 'Thêm mới Nhóm văn bản:';
		$formThem = new Admin_Form_NhomVanBanForm();
		
		$formThem->setMethod('post');
		$formThem->setAction('/zfMHB/public/admin/nhomvanban/them');
		
		$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
		$this->view->form = $formThem;
		
		$formThem->getElement('tennhom')->addValidator('Db_NoRecordExists',true, array(
				'table' => 'tbl_nhomvanban',
				'field' => 'TenNhom'
		));
		$formThem->getElement('tennhom')->getValidator('Db_NoRecordExists')->setMessage('Nhóm đã tồn tại!');
		
		if($this->getRequest()->isPOST())
		{
			if($formThem->isValid($_POST))
			{
				$data = $formThem->getValues();
				$tennhom = $data['tennhom'];
				$nhomvanbanMapper = Library_Model_NhomVanBanMapper::getInstance();
				
				$nhomvanbanObj = new Library_Model_NhomVanBan();
				$nhomvanbanObj->setTenNhom($tennhom);
				
				$nhomvanban = $nhomvanbanMapper->save($nhomvanbanObj);
				$this->_redirect('/admin/nhomvanban/index');
			}
		}
	}
	
	public function capnhatAction()
    {
        $this->view->title = 'Cập nhật nhóm: ';
        $formCapnhat = new Admin_Form_NhomVanBanForm();        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/nhomvanban/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $manhom = $this->getRequest()->getParam('id');

        $formCapnhat->getElement('tennhom')->addValidator('Db_NoRecordExists', true, array(
        		'table' => 'tbl_nhomvanban',
        		'field' => 'TenNhom',
        		'exclude' => array(
        				'field' => 'MaNhom',
        				'value' => $manhom)
        ));
        
     	$formCapnhat->getElement('nhomvanban')->getValidator('Db_NoRecordExists')->setMessage('Nhóm đã tồn tại!');
        $nhomvanbanMapper = Library_Model_NhomVanBanMapper::getInstance();
        $this->view->form = $formCapnhat;
        
       if($this->getRequest()->isPOST())
		{
			if($formCapnhat->isValid($_POST))
			{
				$data = $formCapnhat->getValues();
				
				$id =	$data['id'];
				$tennhom = $data['nhomvanban'];
				
				$nhomvanbanObj = new Library_Model_NhomVanBan();
				
				$nhomvanbanObj->setId($id);
				$nhomvanbanObj->setTenNhom($tennhom);
				
				$nhomvanban = $nhomvanbanMapper->save($nhomvanbanObj);
				$this->_redirect('/admin/nhomvanban/index');
				
			}
		}
		else
		{
			$nhomvanban = $nhomvanbanMapper->searchNhomVanBanById($manhom);

			$formCapnhat->getElement('id')->setValue($nhomvanban->getID());
			$formCapnhat->getElement('nhomvanban')->setValue($nhomvanban->getTennhomvanban());
		}
	}
	
	public function xoaAction()
	{
		$this->_helper->viewRenderer->setNoRender(true);
		$manhom = $this->getRequest()->getParam('id');
		$nhomvanbanMapper = Library_Model_NhomVanBanMapper::getInstance();
		$nhomvanbanMapper->delete($manhom);
		$this->_redirect('/admin/nhomvanban/index');
		
	}
}